#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include"Log.hpp"
using namespace std;

namespace dict_ns
{
    const string defaultpath="./Dict.txt";
    const string SEP=": ";
    class Dict
    {
    private:
        bool Load()
        {
            ifstream in(_dict_cond_path);
            if(!in.is_open())
            {
                LOG(FATAL, "open %s error\n",_dict_cond_path.c_str());
                return false;
            }
            string line;
            while(getline(in,line))
            {
                if(line.empty())    continue;
                auto pos=line.find(SEP);
                if(pos==string::npos)   continue;
                string word=line.substr(0,pos);
                if(word.empty())    continue;
                string han=line.substr(pos+SEP.size());
                if(han.empty())     continue;
                LOG(DEBUG, "load info %s:%s\n",word.c_str(),han.c_str());
                _dict.insert(make_pair(word,han));
            }
            in.close();
            LOG(DEBUG, "load %s sucess!!\n",_dict_cond_path.c_str());
            return true;
        }
    public:
        Dict(const string &path=defaultpath):_dict_cond_path(path)
        {
            Load();
        }
        string Translate(const string& word,bool& ok)
        {
            ok=true;
            auto iter=_dict.find(word);
            if(iter==_dict.end())
            {
                ok=false;
                return "找不到";
            }
            return _dict[word];
        }
        ~Dict()
        {

        }

    private:
        unordered_map<string, string> _dict;
        string _dict_cond_path;
    };
}